package com.demo.springcloud.controller;

import com.demo.springcloud.entities.Dept;
import com.demo.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class DeptServiceController {

	@Autowired
	private DeptService deptService;

	@ResponseBody
	@RequestMapping(value = "/demo/get/{id}", method = RequestMethod.GET)
	public Dept findById(@PathVariable("id") Long id){
		return deptService.findById(id);
	}

	@ResponseBody
	@RequestMapping(value = "/demo/list", method = RequestMethod.GET)
	public List<Dept> findDeptList(){
		return deptService.findDeptList();
	}

	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping(value = "/demo/discovery")
	public void disccovery(){
		List<String> list = discoveryClient.getServices();
		System.out.println(list);

		List<ServiceInstance> serviceInstances = discoveryClient.getInstances("DEMP-PRODUCER");
		for (ServiceInstance instance : serviceInstances) {
			System.out.println(instance.getHost()+ "   " + instance.getPort() + "  " + instance.getServiceId());
		}
	}

}
